package com.api.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.component.domain.Content;
import com.component.domain.MChannel;
import com.component.domain.MModel;
import com.component.domain.vo.AddModelVo;
import com.component.domain.vo.LieSumVo;
import com.component.domain.vo.ModelChannelVo;
import com.component.enumitem.ModelComponentEnum;
import com.component.enumitem.NoteDataTypeEnum;
import com.component.service.ContentService;
import com.component.service.MChannelService;
import com.component.service.MModelService;
import com.system.common.ContextUtils;
import com.system.common.Result;
import com.system.util.EnumToListConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Classname ModelApi
 * @Description TODO
 * @Date 2023/10/23 14:01
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@RestController
@RequestMapping("/model")
public class ModelApi {

    @Autowired
    private MChannelService mChannelServicel;

    @Autowired
    private MModelService mModelService;

    @GetMapping("/getmodelchannel")
    public JSONObject getModelChannel(){
        List<MChannel> mChannels=mChannelServicel.getSystemModelChannel();
        return Result.successInfo(mChannels);
    }

    @GetMapping("getcomponent")
    public JSONObject getComponent(){
        ModelComponentEnum[] components=ModelComponentEnum.values();
        List<Map<String,Object>> maps= EnumToListConverter.convert(components,"componentName","componentType");
        return Result.successInfo(maps);
    }

    @PostMapping("addmodel")
    public JSONObject addModel(@RequestBody AddModelVo vo){
        if(vo.getId()==null){
            MModel mModel=new MModel();
            mModel.setModelTitle(vo.getTitle());
            mModel.setModelJson(JSONObject.toJSONString(vo.getModelVos()));
            mModel.setHanSumField(JSONObject.toJSONString(vo.getHanSumFields()));
            mModel.setLieSumField(JSONObject.toJSONString(vo.getLieSumFields()));
            mModel.setSumtype(vo.getSumType());
            mModelService.save(mModel);
            return Result.successInfo("添加成功");
        }else {
            MModel mModel=mModelService.getById(vo.getId());
            if(mModel==null){
                return Result.errorInfo("请刷新系统后重试");
            }
            if(vo.getTitle()!=null){
                mModel.setModelTitle(vo.getTitle());
            }
            if(vo.getModelVos()!=null){
                mModel.setModelJson(JSONObject.toJSONString(vo.getModelVos()));
            }
            if(vo.getHanSumFields()!=null){
                mModel.setHanSumField(JSONObject.toJSONString(vo.getHanSumFields()));
            }
            if(vo.getLieSumFields()!=null){
                mModel.setLieSumField(JSONObject.toJSONString(vo.getLieSumFields()));
            }
            if(vo.getSumType()!=null){
                mModel.setSumtype(vo.getSumType());
            }
            mModelService.updateById(mModel);
            return Result.successInfo("修改成功");
        }

    }

    @GetMapping("/page")
    public JSONObject page(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                           @RequestParam(value = "title",required = false)String title){
        IPage<MModel> mModelIPage=mModelService.page(pageNum, pageSize, title);
        return Result.successInfo(mModelIPage);
    }

    /**
     * 绑定栏目id
     * @return
     */
    @PostMapping("bindchannelId")
    public JSONObject bindChannelId(@Validated @RequestBody ModelChannelVo modelChannelVo){
        mModelService.bindChannelModel(modelChannelVo);
        return Result.successInfo("绑定成功");
    }

    @GetMapping("getmodefieldlist")
    public JSONObject getModelFieldList(@RequestParam("id")String id){
        MModel mModel=mModelService.getById(id);
        if(mModel!=null){
            JSONArray lie=JSONArray.parseArray(mModel.getLieSumField());
            JSONArray array=JSONArray.parseArray(mModel.getModelJson());
            JSONObject obj=new JSONObject();
            obj.put("lie",lie);
            obj.put("array",array);
            obj.put("sumtype",mModel.getSumtype());
            return Result.successInfo(obj);
        }
        return Result.successInfo(Collections.emptyList());
    }

    @PostMapping("delete")
    public JSONObject delete(@RequestBody JSONObject jsonObject){
        if(!jsonObject.containsKey("id")){
            return Result.errorInfo("删除失败,请稍后再试");
        }
        String id=jsonObject.getString("id");
        mModelService.removeById(id);
        return Result.successInfo("删除成功");
    }

    @Autowired
    private ContentService contentService;


    @GetMapping("getmodeldata")
    public JSONObject getModelData(@RequestParam("contentId")Integer contentId){
        Content content=contentService.getContentByNid(contentId);
        if(content==null){
            return Result.errorInfo("数据错误");
        }
        if(!content.getDatatype().equals(NoteDataTypeEnum.JSON.getDataType())){
            return Result.errorInfo("非json数据");
        }
        JSONArray model=contentService.getModel(content);
        JSONArray array=contentService.getArray(content);
        JSONArray labels=mModelService.getTableLabelByModel(model);
        JSONObject json=new JSONObject();
        json.put("th",labels);
        json.put("td",array);
        return Result.successInfo("",json);
    }

    /**
     * 添加列统计数据
     * @return
     */
    @PostMapping("addliesumfield")
    public JSONObject addLieSumfield(@Validated @RequestBody LieSumVo vo){
        MModel mModel=mModelService.getById(vo.getId());
        List<String> strings=mModelService.getTableFieldsByModel(mModel);
        List<String> list=vo.getFields();
        for (String s : list) {
            if(!strings.contains(s)){
                return Result.errorInfo("包含不存在字段，请刷新系统后重试");
            }
        }
        mModel.setLieSumField(JSONObject.toJSONString(list));
        mModelService.updateById(mModel);
        return Result.successInfo("添加成功");
    }



}
